<?php

namespace App\Http\Controllers\v3;

use App\Models\Cates;
use App\Models\VideoData;
use App\Models\Videos;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 分类相关
 * Class CateController
 * @package App\Http\Controllers
 */
class CateController extends Controller {
    /**
     * 列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(){
        try {
            $lists = Cates::where('option',Cates::OPTION_1)
                ->where('status',Cates::STATUS_1)
                ->orderBy('id','ASC')
                ->get()
                ->toArray();
            return $this->returnJson(0,'',array_chunk($lists,2));
        } catch (\Exception $e) {
            return $this->returnJson(1, '网络超时，请刷新再试');
        }
    }

    /**
     * 详情及统计
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function info(){
        try {
            $id = request('id',0);
            if(empty($id)){
                return $this->returnJson(1,'参数错误');
            }
            //获取分类信息
            $info = Cates::where('status',Cates::STATUS_1)->find($id);
            if(!$info){
                return $this->returnJson(1,'数据信息不存在');
            }
            //统计分类下视频总人气
            //统计分类下视频总个数
            $nums = Videos::where('cate_id',$id)
                ->select(DB::raw('sum(view) as views'),DB::raw('count(id) as ids'))
                ->first();
            $info['count'] = $nums;
            return $this->returnJson(0,'',$info);
        } catch (\Exception $e) {
            return $this->returnJson(1, '网络超时，请刷新再试');
        }
    }

    /**
     * 获取分类下的视频列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function video(){
        try {
            $id = request('id',0);
            if(empty($id)){
                return $this->returnJson(1,'参数错误');
            }
            $lists = Videos::where('cate_id',$id)
                ->select('id','title','thumb','time','view')
                ->where('status',Videos::STATUS_1)
                ->with(['cate' => function ($query) {
                    $query->select('id', 'name','avatar')->get();
                }])
                ->orderBy('sort','DESC')
                ->paginate(15);
            return $this->returnJson(0,'',$lists);
        } catch (\Exception $e) {
            return $this->returnJson(1, '网络超时，请刷新再试');
        }

    }

    public function getVideoCategoryGood(){
        $lists=VideoData::where('is_option',1)
            ->select('id','title','thumb','time','view_num','video_link','cate_id','chanel')
            ->where('status',VideoData::STATUS_YES)
            ->get()
            ->groupBy('chanel');
        $_lists = [];
        foreach ($lists as $key=>$list){
            if(count($list)>2){
                $_lists[$key][] = $list[0];
                $_lists[$key][] = $list[1];
            }else{
                $_lists[$key] = $list;
            }
        }
        return $this->respSuccess('success',$_lists);
    }

}
